import operate from "@/common/operate.js";
import { useUserStore } from "@/store/user.js";
import { getOpenId } from "@/api/login";
const request = (param) => {
  const user = useUserStore();
  // 请求参数
  var url = param.url,
    method = param.method,
    header = {
      clientId: "eWxsY21wJTNBeWxsY21w",
    },
    data = param.data || {};
    hideLoading = param.hideLoading || false;

  //拼接完整请求地址
  var requestUrl = operate.api() + url;

  //请求方式:GET或POST(POST需配置
  // header: {'content-type' : "application/x-www-form-urlencoded"},)
  if (method) {
    method = method.toUpperCase(); //小写改为大写
    // if (method == "POST") {
    //     header['content-type'] = "application/x-www-form-urlencoded"
    // } else {
    //     header['content-type'] = "application/json"
    // }
    header["content-type"] = "application/json";
  }
  if (user.token) {
    header["Authorization"] = `${user.token}`;
  }

  //加载圈
  if (!hideLoading) {
    uni.showLoading({
      title: "加载中...",
    });
  }

  // 返回promise
  return new Promise((resolve, reject) => {
    // 请求
    uni.request({
      url: requestUrl,
      data: data,
      method: method,
      header: header,
      success: (res) => {
        // 判断 请求api 格式是否正确
        if (res.statusCode && res.statusCode != 200) {
          uni.showToast({
            title: "请求失败",
            icon: "none",
          });
          return;
        }
        if (res.data.code === 401) {
          // uni.login({
          //   async success(res) {
          //     if (!res.code) {
          //       console.log("获取登录凭证失败");
          //       return;
          //     }
          //     if (res.errMsg === "login:ok") {
          //       const { code, data, msg } = await getOpenId(res.code);
          //       if (code == 200) {
          //         console.log("data", data);
          //         user.setOpenId(data.openId);
          //         user.setToken(data.token);
          //         user.changeIsSxToken(1);
          //       } else {
          //         uni.showToast({
          //           title: msg || "请求失败",
          //           icon: "error",
          //         });
          //       }
          //     }
          //   },
          // });
          uni.showToast({
              title: '登录过期',
              icon: 'none'
          });
          uni.navigateTo({
              url: '/pages/login/register'
          })
        }
        if (res.data.code === 402) {
          uni.showToast({
            title: "用户未登录",
            icon: "none",
          });
          uni.navigateTo({
            url: "/pages/login/register",
          });
        }
        if (res.data.code != 200&&res.data.code != 401) {
          uni.showToast({
            title: res.data.msg || "请求失败",
            icon: "none",
          });
        }
        // 将结果抛出
        resolve(res.data);
      },
      //请求失败
      fail: (e) => {
        uni.showToast({
          title: e?.data?.msg ?? "请求失败",
          icon: "none",
        });
        resolve(e.data);
      },
      //请求完成
      complete() {
        //隐藏加载
        if (!hideLoading) {
          uni.hideLoading();
        }
        resolve();
        return;
      },
    });
  });
};

export default request;
